<template>
  <div style="border: 1px solid #ccc; width: 720px">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
      @onChange="onChange"
    />
  </div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { imgUploadUrl } from "@/api/upload";
import { upDataImage } from "@/api/wxCode";

export default {
  components: { Editor, Toolbar },
  props: {
    editValue: {
      required: false,
      default: "",
    },
    isDisable: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  watch: {
    editValue() {
      this.html = this.editValue;
    },
  },
  data() {
    return {
      editor: null,
      html: "",
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          // 配置上传图片
          uploadImage: {
            customUpload: this.update,
          },
          // 继续其他菜单配置...
        },
      },
      mode: "default", // or 'simple'
    };
  },
  methods: {
    onCreated(editor) {
      
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
      // console.log(this.editor.getAllMenuKeys())
      // 隐藏一些不需要的插件
      this.toolbarConfig = {
        excludeKeys: [
          "insertVideo",
          "uploadVideo",
          "group-video",
          "fullScreen",
          "insertLink"
        ],
      };
      if (this.isDisable) {
        this.editor.disable();
      }
    },
    // 内容修改事件
    onChange(editor) {
      this.submitData();
    },
    // 提交数据到外部
    submitData() {
      this.$emit("editData", this.html);
    },
    customPaste(editor, event, callback) {
      // console.log("ClipboardEvent 粘贴事件对象", event);
      // // 自定义插入内容
      // editor.insertText("xxx");
      // event.preventDefault();
      // callback(false); // 返回值（注意，vue 事件的返回值，不能用 return）
    },
    // 图片上传
    update(file, insertFn) {
      console.log(file);
      let { size, type, name } = file;
      let typeList = ["jpg", "png", "gif", "jpeg"];
      let imgType = type.split("/")[1];
      let fileSize = size / 1024 / 1024;
      if (typeList.indexOf(imgType) == -1) {
        this.$message.error("仅支持上传jpg/png/gif类型的文件，请重新上传文件");
        return;
      }
      if (fileSize > 2) {
        this.$message.error("图片最大不能超过2MB，请重新上传文件");
        return;
      }
      const formData = new FormData();
      formData.append("file", file);
      upDataImage(formData).then((res) => {
        if (res.code == 0) {
          let imageUrl = res.data.baseUrl + res.data.path;
          insertFn(imageUrl, name, imageUrl);
        }
      });
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.html = this.editValue;
    });
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>